package com.database;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bean.Down;
import com.constant.Constant;

public class Database {
	private String fileName = "/download.db";
	private File file = new File(Constant.DATABASE_PATH + fileName);
	private SQLiteDatabase db;
	
	public Database() {
		
		File catalog = new File(Constant.DATABASE_PATH);
		if(!catalog.exists())
			catalog.mkdirs();
		
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		db = SQLiteDatabase.openOrCreateDatabase(file, null);
		
		createTable();
	} 
	
	public void createTable() {
		
		int aa = 0;
		Cursor cursor = db.rawQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='resource'", null);
		while(cursor.moveToNext()) {
			aa = cursor.getInt(0);
		}
		
		cursor.close();
		if(aa == 1) {
			return;
		}
		
        String sql = "CREATE TABLE resource(id INTEGER PRIMARY KEY AUTOINCREMENT, course VARCHAR(20) NOT NULL," +
				"grade VARCHAR(20) NOT NULL, ispackage INT NOT NULL,resourceName VARCHAR(20) NOT NULL, resourceId VARCHAR(20) NOT NULL)";
        db.execSQL(sql);
       
	} // createTable
	
	public void addResource(final Down resource) {
		String sql = "INSERT INTO resource(course,grade,ispackage,resourceName,resourceId) VALUES('" + resource.getCourse() + "','" + resource.getGrade() + "'," 
					+ resource.getIsPackage() + ",'" + resource.getResourceName() + "','" + resource.getResourceId() + "')";
		db.execSQL(sql);
	}
	
	
//	/**
//	 * 查询数据库中保存的所有下载记录数据
//	 * @return
//	 */
//	public ArrayList<Down> query() {
//		
//		ArrayList<Down> list = new ArrayList<Down>();
//		
//		Cursor cursor = db.rawQuery("SELECT * FROM resource", null);
//		while(cursor.moveToNext()) {
//			Down down = new Down();
//			down.setCourse(cursor.getString(cursor.getColumnIndex("course")));
//			down.setGrade(cursor.getString(cursor.getColumnIndex("grade")));
//			down.setIsPackage(cursor.getInt(cursor.getColumnIndex("ispackage")));
//			down.setResourceName(cursor.getString(cursor.getColumnIndex("resourceName")));
//			down.setResourceId(cursor.getString(cursor.getColumnIndex("resourceId")));
//			
//			list.add(down);
//		} // while
//		
//		return list;
//	}
	
	public HashMap<String, HashMap<String, ArrayList<String>>> query() {
		
		HashMap<String, HashMap<String, ArrayList<String>>> hashMap = new HashMap<String, HashMap<String,ArrayList<String>>>();
		
		Cursor cursor = db.rawQuery("SELECT * FROM resource", null);
		while(cursor.moveToNext()) {
			Down down = new Down();
			down.setCourse(cursor.getString(cursor.getColumnIndex("course")));
			down.setGrade(cursor.getString(cursor.getColumnIndex("grade")));
			down.setIsPackage(cursor.getInt(cursor.getColumnIndex("ispackage")));
			down.setResourceName(cursor.getString(cursor.getColumnIndex("resourceName")));
			down.setResourceId(cursor.getString(cursor.getColumnIndex("resourceId")));
			
			if(down.getIsPackage() == 1)
				continue;
			
			HashMap<String, ArrayList<String>> map = hashMap.get(down.getCourse());
			if(map == null) {
				map = new HashMap<String, ArrayList<String>>();
				hashMap.put(down.getCourse(), map);
			}
			
			ArrayList<String> list = map.get(down.getGrade());
			if(list == null) {
				list = new ArrayList<String>(4);
				map.put(down.getGrade(), list);
			}
			
			int i = 0, len = list.size();
			for( i = 0; i <= len; i++) {
				if(i == len) {
					list.add(down.getResourceName());
					break;
				}
				
				if(down.getResourceName().equals(list.get(i))) 
					break;
			}
			
//			if(i != len || i == 0)
//				list.add(down.getResourceName());
		}
		cursor.close();
		
		return hashMap;
	}
	
	
	
//	public HashMap<String, HashMap<String, Down>> query() {
//		
//		// HashMap<Course, HashMap<Grade, Down>>
//		HashMap<String, HashMap<String, Down>> hashMap = new HashMap<String, HashMap<String,Down>>();
//		
//		
//		Cursor cursor = db.rawQuery("SELECT * FROM resource", null);
//		while(cursor.moveToNext()) {
//			Down down = new Down();
//			down.setCourse(cursor.getString(cursor.getColumnIndex("course")));
//			down.setGrade(cursor.getString(cursor.getColumnIndex("grade")));
//			down.setIsPackage(cursor.getInt(cursor.getColumnIndex("ispackage")));
//			down.setResourceName(cursor.getString(cursor.getColumnIndex("resourceName")));
//			down.setResourceId(cursor.getString(cursor.getColumnIndex("resourceId")));
//			
//			HashMap<String, Down> map = hashMap.get(down.getCourse());
//			if(map == null) {
//				map = new HashMap<String, Down>();
//				hashMap.put(down.getCourse(), map);
//			}
//			
//			Down down2 = map.get(down.getGrade());
//			if(down2 == null)
//				map.put(down.getGrade(), down2);
//			
//		} // while
//		
//		cursor.close();
//		
//		return hashMap;
//	}
	
	public void close() {
		db.close();
	}
}
